﻿using System;
using System.Collections.Generic;
using System.Linq;
using FlashPay.Entity;
using FlashPay.Entity.Parameter;
using FlashPay.Entity.Request.DepositMatchRecord;
using FlashPay.Entity.Response.User;
using FlashPay.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FlashPay.Admin.Controllers
{
    public class DepositMatchRecordController : BaseController
    {
        #region 注入
        private readonly DepositMatchRecordService _depositMatchRecordService;
        public DepositMatchRecordController(IAuthenticate<TicketResponse> _manage, IHttpContextAccessor _accessor, DepositMatchRecordService depositMatchRecordService) : base(_manage, _accessor)
        {
            _depositMatchRecordService = depositMatchRecordService;
        }
        #endregion


        /// <summary>
        /// 获取收款匹配记录列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.ReceiptMatchList001)] 
        public JsonResult GetDepositMatchRecord(DepositMatchRecordQuery query)
        {
            if (!string.IsNullOrEmpty(query.StartTime) && !string.IsNullOrEmpty(query.EndTime))
            {
                if (this.IsDate(query.StartTime) && this.IsDate(query.EndTime))
                {
                    query.StartTime = Convert.ToDateTime(query.StartTime).ToString("yyyy-MM-dd HH:mm:ss");
                    query.EndTime = Convert.ToDateTime(query.EndTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                    query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                }
            }
            else
            {
                query.StartTime = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
                query.EndTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            }

            query.CompanyId = _manage.data.CompanyID;
            query.UserPermission = _manage.data.UserPermission;

            var response = _depositMatchRecordService.GetDepositMatchRecordList(query);

            return Json(response);
        }

        private bool IsDate(string date)
        {
            try
            {
                DateTime.Parse(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //根据ID查询详细信息
        public JsonResult GetMatchRecordDetail(DepositMatchRecordQuery query)
        {
            var response = _depositMatchRecordService.GetMatchRecordDetail(query);
            return Json(response);
        }

        /// <summary>
        /// 根据规则信息查询收款记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        //[AuthorizeFilter(AuthCode.ReceiptMatchList005)]
        public JsonResult GetRecordListByRuleInfo(DepositMatchRecordQuery query)
        {
            var response = _depositMatchRecordService.GetRecordListByRuleInfo(query);
            return Json(response);
        }

        /// <summary>
        /// 收款记录查询(规则)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetMatchSearch(GetMatchSearchRequest request)
        {
            var response = _depositMatchRecordService.GetMatchSearch(request);
            return Json(response);
        }

        /// <summary>
        /// 收款匹配记录[绑定收款记录]
        /// </summary>
        /// <param name="query">参数</param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.ReceiptMatchList005)]
        public JsonResult ReceiptMatchRecordBindReceiptRecord(ReceiptMatchRecordBindReceiptRecordRequest request)
        {
            var response = _depositMatchRecordService.ReceiptMatchRecordBindReceiptRecord(request);
            return Json(response);
        }


        [AuthorizeFilter(AuthCode.ReceiptMatchList001)]
        public JsonResult GetDepositMatchRecordAdd()
        {
            var response = _depositMatchRecordService.GetDepositMatchRecordAdd(new DepositMatchRecordAddRequest() {
                CompanyId = _manage.data.CompanyID,
                UserPermission = _manage.data.UserPermission
            });


            return Json(response);
        }

        [AuthorizeFilter(AuthCode.ReceiptMatchList001)]
        public JsonResult GetDepositMatchRecordById(long id)
        {
            var response = _depositMatchRecordService.GetDepositMatchRecordById(new DepositMatchRecordAddRequest()
            {
                Id =id,
                CompanyId = _manage.data.CompanyID,
                UserPermission = _manage.data.UserPermission
            });


            return Json(response);
        }

        /// <summary>
        /// 新增收款匹配记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.ReceiptMatchList007)]
        public JsonResult Add(AddRequest request)
        {
            var response = _depositMatchRecordService.Add(request);
            return Json(response);
        }

        /// <summary>
        /// 新增收款匹配记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AuthorizeFilter(AuthCode.ReceiptMatchList007)]
        public JsonResult Edit(EditRequest request)
        {
            var response = _depositMatchRecordService.Edit(request);
            return Json(response);
        }
    }
}